package cn.cc.common.mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @program: smail-mi-cloud
 * @description: mq配置
 * @author: Mr.WuChao
 * @create: 2021-07-26 10:15
 **/
@Component
public class MQConfig {

    /**
     * 创建延时队列
     */
    @Bean("orderDelayQueue")
    public Queue delayQueue() {
        return QueueBuilder.durable("orderDelayQueue")
                //消息过时将消息发送到order-dlx-queue队列
                .withArgument("x-dead-letter-exchange", "order-dlx-queue")
                // 消息过时将消息发送到order-routing-delay路由
                .withArgument("x-dead-letter-routing-key", "order-routing-delay").build();
    }

    /**
     * 创建死信队列
     */
    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable("order-dlx-queue").build();
    }

    /**
     * 创建死信交换机
     */
    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange("order-dlx-queue").build();
    }


    /**
     * 绑定死信队列和交换机
     */
    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxExchange") Exchange dlxExchange
            , @Qualifier("dlxQueue") Queue dlxQueue) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange)
                .with("order-routing-delay").noargs();
    }
}
